//package com.life.notes.config;
//
//import com.life.notes.config.interceptor.BaseContextHandler;
//import feign.Feign;
//import feign.Logger;
//import feign.RequestInterceptor;
//import lombok.extern.slf4j.Slf4j;
//import okhttp3.ConnectionPool;
//import org.springframework.boot.autoconfigure.AutoConfigureBefore;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
//import org.springframework.cloud.openfeign.FeignAutoConfiguration;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Enumeration;
//import java.util.concurrent.TimeUnit;
//
//@Slf4j
//@Configuration
//@ConditionalOnClass(Feign.class)
//@AutoConfigureBefore(FeignAutoConfiguration.class)
//public class FeignOkHttpConfig {
//
//    @Bean
//    public RequestInterceptor authRequestInterceptor() {
//        return requestTemplate -> {
//            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//            if (attributes != null) {
//                HttpServletRequest request = attributes.getRequest();
//                Enumeration<String> headerNames = request.getHeaderNames();
//                if (headerNames != null) {
//                    while (headerNames.hasMoreElements()) {
//                        String name = headerNames.nextElement();
//                        if (name.toUpperCase().startsWith("X-")) {
//                            String values = request.getHeader(name);
//                            log.info("微服务内部调用feign传递请求头:" + name + ":" + values);
//                            requestTemplate.header(name, values);
//                        }
//                    }
//                }
//            }
//
//            Integer userId = BaseContextHandler.getUserId();
//            log.info("userId:" + userId);
//            requestTemplate.header("userId", userId.toString());
//        };
//    }
//
//    @Bean
//    public okhttp3.OkHttpClient okHttpClient() {
//        return new okhttp3.OkHttpClient.Builder()
//                .readTimeout(60, TimeUnit.SECONDS)
//                .connectTimeout(60, TimeUnit.SECONDS)
//                .writeTimeout(120, TimeUnit.SECONDS)
//                .connectionPool(new ConnectionPool())
//                .build();
//    }
//
//    @Bean
//    Logger.Level feignLoggerLevel() {
//        return Logger.Level.FULL;
//    }
//}